#include "zlang_in.h"

struct ZlangDirectProperty_zruntime
{
	int		dummy ;
} ;

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetOsType;
int ZlangInvokeFunction_zruntime_GetOsType( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
#if defined(__linux__)
	CallRuntimeFunction_string_SetStringValue( rt , out1 , OS_TYPE_LINUX , STRLEN_OF_STRING );
#elif defined(_WIN32)
	CallRuntimeFunction_string_SetStringValue( rt , out1 , OS_TYPE_WINDOWS , STRLEN_OF_STRING );
#endif
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_QueryGlobalObject_string;
int ZlangInvokeFunction_zruntime_QueryGlobalObject_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char					*query_obj_name = NULL ;
	struct ZlangObject			*query_obj = NULL ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in1 , & query_obj_name , NULL );
	
	query_obj = QueryGlobalObjectByObjectName( rt , query_obj_name ) ;
	if( query_obj == NULL )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	ReferObject( rt , out1 , query_obj );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_QueryLocalObject_string;
int ZlangInvokeFunction_zruntime_QueryLocalObject_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char					*query_obj_name = NULL ;
	struct ZlangObject			*query_obj = NULL ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in1 , & query_obj_name , NULL );
	
	query_obj = QueryObjectByObjectNameIncludeLowerLocalObjectsStack( rt , query_obj_name , NULL ) ;
	if( query_obj == NULL )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	ReferObject( rt , out1 , query_obj );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_QueryPropertyInObject_object_string;
int ZlangInvokeFunction_zruntime_QueryPropertyInObject_object_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	char					*query_prop_name = NULL ;
	struct ZlangObject			*query_prop = NULL ;
	
	CallRuntimeFunction_string_GetStringValue( rt , in2 , & query_prop_name , NULL );
	
	query_prop = QueryPropertyInObjectByPropertyName( rt , in1 , query_prop_name ) ;
	if( query_prop == NULL )
	{
		UnreferObject( rt , out1 );
		return 0;
	}

	ReferObject( rt , out1 , query_prop );

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetZlangPathfilename;
int ZlangInvokeFunction_zruntime_GetZlangPathfilename( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	CallRuntimeFunction_string_SetStringValue( rt , out1 , GetZlangPathfilename(rt) , STRLEN_OF_STRING );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetZlangPathname;
int ZlangInvokeFunction_zruntime_GetZlangPathname( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	CallRuntimeFunction_string_SetStringValue( rt , out1 , GetZlangPathname(rt) , STRLEN_OF_STRING );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetZlangFilename;
int ZlangInvokeFunction_zruntime_GetZlangFilename( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	CallRuntimeFunction_string_SetStringValue( rt , out1 , GetZlangFilename(rt) , STRLEN_OF_STRING );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetFirstZFilePathname;
int ZlangInvokeFunction_zruntime_GetFirstZFilePathname( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	CallRuntimeFunction_string_SetStringValue( rt , out1 , GetFirstZFilePathname(rt) , STRLEN_OF_STRING );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetCurrentZFilename;
int ZlangInvokeFunction_zruntime_GetCurrentZFilename( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	if( rt == NULL || rt->travel_token_info == NULL || rt->travel_token_info->source_filename == NULL )
		CallRuntimeFunction_string_SetStringValue( rt , out1 , "" , 0 );
	else
		CallRuntimeFunction_string_SetStringValue( rt , out1 , rt->travel_token_info->source_filename , STRLEN_OF_STRING );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetCurrentZFileRow;
int ZlangInvokeFunction_zruntime_GetCurrentZFileRow( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	if( rt == NULL || rt->travel_token_info == NULL )
		CallRuntimeFunction_int_SetIntValue( rt , out1 , 0 );
	else
		CallRuntimeFunction_int_SetIntValue( rt , out1 , rt->travel_token_info->source_row );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetCurrentZFileColumn;
int ZlangInvokeFunction_zruntime_GetCurrentZFileColumn( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	
	if( rt == NULL || rt->travel_token_info == NULL )
		CallRuntimeFunction_int_SetIntValue( rt , out1 , 0 );
	else
		CallRuntimeFunction_int_SetIntValue( rt , out1 , rt->travel_token_info->source_col );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetCurrentObjectName;
int ZlangInvokeFunction_zruntime_GetCurrentObjectName( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObjectsStackFrame		*prev_func_objs_stack_frame = NULL ;
	
	prev_func_objs_stack_frame = GetPreviousFunctionObjectsStackFrame( rt ) ;
	if( prev_func_objs_stack_frame == NULL )
		CallRuntimeFunction_string_SetStringValue( rt , out1 , "" , 0 );
	else
		CallRuntimeFunction_string_SetStringValue( rt , out1 , GetObjectsStackInObjectName(prev_func_objs_stack_frame) , STRLEN_OF_STRING );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetCurrentFunctionName;
int ZlangInvokeFunction_zruntime_GetCurrentFunctionName( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObjectsStackFrame		*prev_func_objs_stack_frame = NULL ;
	
	prev_func_objs_stack_frame = GetPreviousFunctionObjectsStackFrame( rt ) ;
	if( prev_func_objs_stack_frame == NULL )
		CallRuntimeFunction_string_SetStringValue( rt , out1 , "" , 0 );
	else
		CallRuntimeFunction_string_SetStringValue( rt , out1 , GetObjectsStackFullFuncName(prev_func_objs_stack_frame) , STRLEN_OF_STRING );
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetInputParametersCount;
int ZlangInvokeFunction_zruntime_GetInputParametersCount( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObjectsStackFrame		*previous_function_local_objs_stack_frame = NULL ;
	int32_t					param_count ;
	
	previous_function_local_objs_stack_frame = GetPreviousFunctionObjectsStackFrame( rt ) ;
	
	param_count = (int)GetInputParameterCountInStackFrame( rt , previous_function_local_objs_stack_frame ) ;
	CallRuntimeFunction_int_SetIntValue( rt , out1 , param_count );
	
	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_zruntime_GetInputParameter;
int ZlangInvokeFunction_zruntime_GetInputParameter( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject			*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject			*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObjectsStackFrame		*previous_function_local_objs_stack_frame = NULL ;
	int32_t					param_count ;
	int32_t					param_no ;
	struct ZlangObject			*param_obj = NULL ;
	int					nret = 0 ;
	
	previous_function_local_objs_stack_frame = GetPreviousFunctionObjectsStackFrame( rt ) ;
	
	param_count = (int)GetInputParameterCountInStackFrame( rt , previous_function_local_objs_stack_frame ) ;
	CallRuntimeFunction_int_GetIntValue( rt , in1 , & param_no );
	if( param_no < 1 || param_no > param_count )
	{
		UnreferObject( rt , out1 ) ;
		return ThrowErrorException( rt , ZLANG_ERROR_PARAMETER , EXCEPTION_MESSAGE_PARAMETER_INVALID );
	}
	param_obj = GetInputParameterInStackFrame( rt , previous_function_local_objs_stack_frame , (int)param_no ) ;
	if( param_obj == NULL )
	{
		UnreferObject( rt , out1 ) ;
		return ThrowFatalException( rt , nret , EXCEPTION_MESSAGE_GENERAL_ERROR );
	}
	nret = ReferObject( rt , out1 , param_obj ) ;
	if( nret )
	{
		TEST_RUNTIME_DEBUG_THEN_PRINT( rt , "ReferObject failed[%d]" , nret )
		return ThrowFatalException( rt , nret , EXCEPTION_MESSAGE_GENERAL_ERROR );
	}
	
	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_zruntime;
void *ZlangCreateDirectProperty_zruntime( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_zruntime	*obj_direct_prop = NULL ;
	
	obj_direct_prop = (struct ZlangDirectProperty_zruntime *)ZLMALLOC( sizeof(struct ZlangDirectProperty_zruntime) ) ;
	if( obj_direct_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( obj_direct_prop , 0x00 , sizeof(struct ZlangDirectProperty_zruntime) );
	
	return (void *)obj_direct_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_zruntime;
void ZlangDestroyDirectProperty_zruntime( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_zruntime	*obj_direct_prop = GetObjectDirectProperty(obj) ;
	
	ZLFREE( obj_direct_prop );
	
	return;
}

static struct ZlangDirectFunctions direct_funcs_zruntime =
	{
		ZLANG_OBJECT_zruntime ,
		
		ZlangCreateDirectProperty_zruntime ,
		ZlangDestroyDirectProperty_zruntime ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		NULL ,
		
		NULL ,
	} ;

ZlangImportObjectFunction ZlangImportObject_zruntime;
struct ZlangObject *ZlangImportObject_zruntime( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangObject	*prop = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_zruntime , & direct_funcs_zruntime , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	prop = AddPropertyInObject( rt , obj , QueryGlobalObjectByObjectName(rt,"string") , "OS_TYPE_LINUX") ;
	if( prop == NULL )
		return NULL;
	CallRuntimeFunction_string_SetStringValue( rt , prop , OS_TYPE_LINUX , STRLEN_OF_STRING );
	SetConstantObject( prop );
	
	prop = AddPropertyInObject( rt , obj , QueryGlobalObjectByObjectName(rt,"string") , "OS_TYPE_WINDOWS") ;
	if( prop == NULL )
		return NULL;
	CallRuntimeFunction_string_SetStringValue( rt , prop , OS_TYPE_WINDOWS , STRLEN_OF_STRING );
	SetConstantObject( prop );
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetOsType , ZLANG_OBJECT_string , "GetOsType" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetZlangPathfilename , ZLANG_OBJECT_string , "GetZlangPathfilename" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetZlangPathname , ZLANG_OBJECT_string , "GetZlangPathname" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetZlangFilename , ZLANG_OBJECT_string , "GetZlangFilename" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetFirstZFilePathname , ZLANG_OBJECT_string , "GetFirstZFilePathname" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetCurrentZFilename , ZLANG_OBJECT_string , "GetCurrentZFilename" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetCurrentZFileRow , ZLANG_OBJECT_int , "GetCurrentZFileRow" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetCurrentZFileColumn , ZLANG_OBJECT_int , "GetCurrentZFileColumn" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetCurrentObjectName , ZLANG_OBJECT_string , "GetCurrentObjectName" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetCurrentFunctionName , ZLANG_OBJECT_string , "GetCurrentFunctionName" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_QueryGlobalObject_string , ZLANG_OBJECT_object , "QueryGlobalObject" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_QueryLocalObject_string , ZLANG_OBJECT_object , "QueryLocalObject" , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_QueryPropertyInObject_object_string , ZLANG_OBJECT_object , "QueryPropertyInObject" , ZLANG_OBJECT_object,NULL , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetInputParametersCount , ZLANG_OBJECT_int , "GetInputParametersCount" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_zruntime_GetInputParameter , ZLANG_OBJECT_object , "GetInputParameter" , ZLANG_OBJECT_int,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj ;
}

